package ls.dom4j.erp.student.dao.impl;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import ls.dom4j.erp.student.constant.StudentConstant;
import ls.dom4j.erp.student.dao.StudentDao;
import ls.dom4j.erp.student.entity.StudentEntity;

public class StudentDaoImpl implements StudentDao {

	@Override
	public void add(StudentEntity entity) throws Exception {

		Document document = DocumentHelper.createDocument();
		Element root = document.addElement(StudentConstant.STUDENTS);
		Element studentElement = root.addElement(StudentConstant.STUDENT_NAME);
		studentElement.addAttribute(StudentConstant.STUDENT_ID, Integer.toString(entity.getStudentId()));
		Element nameElement = studentElement.addElement("name");
		nameElement.setText(entity.getName());
		
		//美化打印
		OutputFormat format=new OutputFormat();
		format.setEncoding("utf-8");
		format.setIndent(true);
		format.setNewlines(true);
		format.setIndent("\t");
		
		XMLWriter writer=new XMLWriter(new FileWriter("src/student2.xml"),format);
		writer.write(document);
		writer.flush();
		writer.close();

		
	}

	

	@SuppressWarnings("unchecked")
	@Override
	public void update(int id, String name) throws Exception {
		
		int counter=0;
		SAXReader reader=new SAXReader();
		Document document = reader.read(new File(StudentConstant.STUDENT_PATH));
		Element root = document.getRootElement();
		List<Element> studentList = root.elements();
		Iterator<Element> iterator = studentList.iterator();
		while(iterator.hasNext()){
			Element studentElement = iterator.next();
			
			if(studentElement.attribute("id").getValue().equals(Integer.toString(id))){
				studentElement.element("name").setText(name);
				System.out.println(studentElement.element("name").getText());
				counter++;
				break;
				
			}
		}
		
		if(counter==0){
			System.out.println("更新失败，因为不存在这个学生");
			
		}else{
			//美化打印
			OutputFormat format=new OutputFormat();
			format.setEncoding("utf-8");
			format.setIndent(true);
			format.setNewlines(true);
			format.setIndent("\t");
			
			XMLWriter writer=new XMLWriter(new FileWriter("src/student2.xml"),format);
			writer.write(document);
			writer.flush();
			writer.close();
			
		}
		
	}

	@Override
	public void findByid(int id) throws Exception {
		// TODO Auto-generated method stub
		
	}



	@SuppressWarnings("unchecked")
	@Override
	public void deleteByname(String StudentName) throws Exception {
		
		int counter=0;
		//1.遍历xml所有的学生
		
		//1.1读取xml,找到student的标签
		SAXReader reader=new SAXReader();
		Document document = reader.read(new File(StudentConstant.STUDENT_PATH));
		Element root = document.getRootElement();
		List<Element> studentList = root.elements();
		for (Element element : studentList) {
			if(element.element("name").equals("StudentName")){
				root.remove(element);	
				counter++;
				break;
			}
		}
		if(counter==0){
			System.out.println("删除失败，因为不存在这个学生");
			
		}else{
			//美化打印
			OutputFormat format=new OutputFormat();
			format.setEncoding("utf-8");
			format.setIndent(true);
			format.setNewlines(true);
			format.setIndent("\t");
			
			XMLWriter writer=new XMLWriter(new FileWriter("src/student2.xml"),format);
			writer.write(document);
			writer.flush();
			writer.close();
			
		}
		
		
		//2.判断学生的姓名是否一致,如果一个都不存在提示学生不存在
		
		
		//3.
		
		
	}

	

}
